package com.xuecheng.content.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xuecheng.commons.constants.CourseConstants;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.BindTeachplanMediaDto;
import com.xuecheng.commons.model.vo.MediaVo;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.commons.utils.AuthInfoHolder;
import com.xuecheng.content.domain.CourseBase;
import com.xuecheng.content.domain.Teachplan;
import com.xuecheng.content.domain.TeachplanMedia;
import com.xuecheng.content.mapper.TeachplanMediaMapper;
import com.xuecheng.content.service.CourseBaseService;
import com.xuecheng.content.service.TeachplanMediaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.media.api.MediaClient;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-07-30
 */
@Service
public class TeachplanMediaServiceImpl extends ServiceImpl<TeachplanMediaMapper, TeachplanMedia> implements TeachplanMediaService {

    @Autowired
    private TeachplanService teachplanService;

    @Autowired
    private CourseBaseService courseBaseService;

    @Autowired
    private MediaClient mediaClient;
    // 媒资绑定
    @Override
    public ResponseResult bangding(BindTeachplanMediaDto dto) {
        // 参数校验
        if (ObjectUtil.isEmpty(dto.getMediaId())||ObjectUtil.isEmpty(dto.getTeachplanId())){
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        // 判断课程的状态
        // 首先传过来的参数中有 课程计划的id  根据id咱们去查询 课程计划  在根据课程计划的中课程表示id  去查询课程是否是上限状态
        Teachplan teachplan = teachplanService.getById(dto.getTeachplanId());
        CourseBase courseBase = courseBaseService.getById(teachplan.getCourseId());
        String auditStatus = courseBase.getAuditStatus();
        if (StrUtil.equals(auditStatus, CourseConstants.AUDIT_COMMIT_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PASTED_STATUS) ||
                StrUtil.equals(auditStatus, CourseConstants.AUDIT_PUBLISHED_STATUS)) {
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        // 通过fegin远程调用
        ResponseResult<MediaVo> voResponseResult = mediaClient.findByID(dto.getMediaId());
        //在判断返回的这个VO的状态码
        if (voResponseResult.getCode()!=0){
            throw new BusinessException(ErrorCode.NOTFOUND);
        }
        // 在取出远程调用的数据     因为返回的是一个JSON格式的 ResponseResult  要得到数据就得getData
        MediaVo Media = voResponseResult.getData();

        // 封装 媒资绑定对象
        TeachplanMedia teachplanMedia = new TeachplanMedia();
        teachplanMedia.setMediaId(Media.getId()); // 媒资信息id
        teachplanMedia.setTeachplanId(teachplan.getId());  // 课程计划id
        teachplanMedia.setCourseId(courseBase.getId()); // 课程表示id
        teachplanMedia.setMediaFilename(Media.getFilename()); //媒资源文件名称
        teachplanMedia.setCreateDate(DateUtil.date()); // 创建时间
        teachplanMedia.setCreatePeople(AuthInfoHolder.getUserId().toString()); // 创建人
        // 调用新增的方法
        this.save(teachplanMedia);
        // 返回结果
        return ResponseResult.okResult();
    }
    // 媒资解绑
    @Override
    public ResponseResult meizijiebang(Long courseBaseId, Long teachPlanMediaId) {
    //校验参数
        if (courseBaseId==null || teachPlanMediaId==null){
            throw new BusinessException(ErrorCode.PARAMSERROR);
        }
        // 根据课程的id 查找状态
        CourseBase base = courseBaseService.getById(courseBaseId);
        // 判断状态是否可以解除绑定  要是发布审核通过的就不能删除了
        if (StrUtil.equals(base.getAuditStatus(),CourseConstants.AUDIT_PASTED_STATUS)||
          StrUtil.equals(base.getAuditStatus(),CourseConstants.AUDIT_COMMIT_STATUS)||
          StrUtil.equals(base.getAuditStatus(),CourseConstants.AUDIT_PUBLISHED_STATUS)){
            throw new BusinessException(ErrorCode.UPDATEEERROR);
        }
        //那就删除
        this.removeById(teachPlanMediaId);


        return ResponseResult.okResult();
    }
}
